import UIKit

class APPopupView: UIView {
    
    class func hidden(completion: (() -> Void)? = nil) {
        let subView:UIView? = UIApplication.shared.windows.first(where: { $0.isKeyWindow })
        for sub in subView?.subviews ?? [] {
            if sub is APPopupView {
                let sub1:APPopupView = sub as! APPopupView
                sub1.dismiss(completion: completion)
            }
        }
    }
    
    enum PopupPosition {
        case center
        case bottom
    }
    
    private var contentView: UIView
    private var dimmingView: UIView
    private var position: PopupPosition
    private var contentWidth: CGFloat
    private var contentHeight: CGFloat
    
    init(contentView: UIView, position: PopupPosition = .center, contentWidth: CGFloat = 250, contentHeight: CGFloat = 200) {
        self.contentView = contentView
        self.position = position
        self.contentWidth = contentWidth
        self.contentHeight = contentHeight
        self.dimmingView = UIView()
        super.init(frame: UIScreen.main.bounds)
        
        setupViews()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setupViews() {
        // 设置遮罩层
        dimmingView.backgroundColor = UIColor.black.withAlphaComponent(0.5)
        dimmingView.alpha = 0
        dimmingView.frame = self.bounds
        addSubview(dimmingView)
        
        // 添加点击手势识别器
        let tapGesture = UITapGestureRecognizer(target: self, action: #selector(dimmingViewTapped))
        dimmingView.addGestureRecognizer(tapGesture)
        
        // 设置内容视图
        addSubview(contentView)
        
        // 设置内容视图的初始位置和大小
        let screenWidth = UIScreen.main.bounds.width
        let screenHeight = UIScreen.main.bounds.height
        
        switch position {
        case .center:
            contentView.frame = CGRect(x: (screenWidth - contentWidth) / 2, y: (screenHeight - contentHeight) / 2, width: contentWidth, height: contentHeight)
            contentView.alpha = 0
        case .bottom:
            contentView.frame = CGRect(x: 0, y: screenHeight, width: screenWidth, height: contentHeight)
        }
    }
    
    @objc private func dimmingViewTapped() {
        dismiss() // 点击背景时关闭弹出视图
    }
    
    func show(in parentView: UIView? = UIApplication.shared.windows.first(where: { $0.isKeyWindow })) {
        guard let parentView = parentView else { return }
        parentView.addSubview(self)
        
        // 动画弹出
        UIView.animate(withDuration: 0.3, animations: {
            self.dimmingView.alpha = 1
            if self.position == .bottom {
                self.contentView.frame.origin.y = UIScreen.main.bounds.height - self.contentHeight
            } else if self.position == .center {
                self.contentView.alpha = 1
            }
        })
    }
    
    func dismiss(completion: (() -> Void)? = nil) {
        UIView.animate(withDuration: 0.3, animations: {
            self.dimmingView.alpha = 0
            if self.position == .bottom {
                self.contentView.frame.origin.y = UIScreen.main.bounds.height
            } else if self.position == .center {
                self.contentView.alpha = 0
            }
        }, completion: { _ in
            self.removeFromSuperview()
            completion?()  // 如果有回调，执行它
        })
    }
}


extension UIButton {
    // 设置按钮的图片在上，文字在下
    func setImageAboveText(spacing: CGFloat = 8) {
        
        // 确保图片和文字的中心对齐
        self.titleLabel?.textAlignment = .center
        
        // 计算并设置图片和文字的间距
        let imageSize = self.currentImage?.size ?? CGSize.zero
        let titleSize = self.titleLabel?.intrinsicContentSize ?? CGSize.zero
        
        // 设置图片位置，确保图片位于上面
        self.imageEdgeInsets = UIEdgeInsets(top: -titleSize.height - spacing / 2, left: 0, bottom: 0, right: -titleSize.width)
        
        // 设置文字位置，确保文字位于下面
        self.titleEdgeInsets = UIEdgeInsets(top: 0, left: -imageSize.width, bottom: -imageSize.height - spacing / 2, right: 0)
        
        // 调整按钮的内容区域，使得按钮能够显示完整
        self.contentEdgeInsets = UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0)
    }
}
